package me.zhengjie.modules.equipment.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.equipment.domain.TWarehouse;
import me.zhengjie.modules.equipment.service.TWarehouseService;
import me.zhengjie.modules.equipment.service.dto.TWarehouseQueryCriteria;
import me.zhengjie.utils.HttpResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
* @author sw
* @date 2019-06-30
*/
@RestController
@RequestMapping("api")
public class TWarehouseController {

    @Autowired
    private TWarehouseService tWarehouseService;

    @Log("查询TWarehouse")
    @GetMapping(value = "/tWarehouse")
    @PreAuthorize("hasAnyRole('ADMIN','TWAREHOUSE_ALL','TWAREHOUSE_SELECT')")
    public ResponseEntity getTWarehouses(TWarehouseQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity(tWarehouseService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("新增TWarehouse")
    @PostMapping(value = "/tWarehouse")
    @PreAuthorize("hasAnyRole('ADMIN','TWAREHOUSE_ALL','TWAREHOUSE_CREATE')")
    public ResponseEntity create(@Validated @RequestBody TWarehouse resources){
        return new ResponseEntity(tWarehouseService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改TWarehouse")
    @PutMapping(value = "/tWarehouse")
    @PreAuthorize("hasAnyRole('ADMIN','TWAREHOUSE_ALL','TWAREHOUSE_EDIT')")
    public ResponseEntity update(@Validated @RequestBody TWarehouse resources){
        tWarehouseService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除TWarehouse")
    @DeleteMapping(value = "/tWarehouse/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','TWAREHOUSE_ALL','TWAREHOUSE_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id){
        try{
            tWarehouseService.delete(id);
            return new ResponseEntity(HttpStatus.OK);
        } catch (Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex);
        }
    }
}